package com.csjung.games.gamenori.server.service.impl;

import com.google.appengine.api.channel.ChannelMessage;
import com.google.appengine.api.channel.ChannelService;
import com.google.appengine.api.channel.ChannelServiceFactory;
import com.google.appengine.api.memcache.MemcacheService;
import com.google.appengine.api.memcache.MemcacheServiceFactory;
import com.google.appengine.api.users.UserServiceFactory;

import com.csjung.games.gamenori.server.MemcacheKey;
import com.csjung.games.gamenori.server.domain.GamenoriChannel;
import com.csjung.games.gamenori.server.service.GamenoriChannelService;
import com.csjung.games.gamenori.shared.ChannelMsgs;

import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service("ChannelService")
public class ChannelServiceImpl implements GamenoriChannelService {

  @Override
  public GamenoriChannel createChannel(String channelType, String userId) {
    ChannelService channelService = ChannelServiceFactory.getChannelService();
    com.google.appengine.api.users.User currentUser =
        UserServiceFactory.getUserService().getCurrentUser();
    Date d = new Date();
    String id = String.valueOf(d.getTime());
    GamenoriChannel channel = new GamenoriChannel();
    channel.setChannelId(id);
    channel.setCreateUserId(currentUser.getUserId());
    channel.setContactUserId(userId);
    channel.setChannelType(channelType);

    // 상대방에게 알림
    channelService.sendMessage(new ChannelMessage(userId, ChannelMsgs.REQUEST_CHAT));
    return channel;
  }

  @Override
  public String sendMsgToChannel(GamenoriChannel channel, String message) {
    ChannelService channelService = ChannelServiceFactory.getChannelService();
    com.google.appengine.api.users.User currentUser =
        UserServiceFactory.getUserService().getCurrentUser();
    String toUserId = "";
    if (currentUser.getUserId().equals(channel.getCreateUserId())) {
      toUserId = channel.getContactUserId();
    } else {
      toUserId = channel.getCreateUserId();
    }
    channel.setMessage(message);
    channel.setChannelType(ChannelMsgs.MESSAGE);
    channelService.sendMessage(new ChannelMessage(toUserId, channel.getJsonString()));
    return channel.getChannelId();
  }

  @Override
  @SuppressWarnings("unchecked")
  public GamenoriChannel requestChat(String contactUserId) {
    ChannelService channelService = ChannelServiceFactory.getChannelService();
    com.google.appengine.api.users.User currentUser =
        UserServiceFactory.getUserService().getCurrentUser();

    Date d = new Date();
    String id = String.valueOf(d.getTime());
    GamenoriChannel channel = new GamenoriChannel();
    channel.setChannelId(id);
    channel.setCreateUserId(currentUser.getUserId());
    channel.setContactUserId(contactUserId);
    channel.setChannelType(ChannelMsgs.REQUEST_CHAT);
    MemcacheService mcs = MemcacheServiceFactory.getMemcacheService();
    Map<String, GamenoriChannel> channels =
        (Map<String, GamenoriChannel>) mcs.get(MemcacheKey.CHANNELS_KEY);
    if (channels == null) {
      channels = new HashMap<String, GamenoriChannel>();
    }
    channels.put(id, channel);
    mcs.put(MemcacheKey.CHANNELS_KEY, channels);
    // 상대방에게 알림
    channelService.sendMessage(new ChannelMessage(channel.getContactUserId(), channel.getJsonString()));
    return channel;
  }

  @Override
  @SuppressWarnings("unchecked")
  public GamenoriChannel getChannel(String channelId) {
    GamenoriChannel channel = null;
    MemcacheService mcs = MemcacheServiceFactory.getMemcacheService();
    Map<String, GamenoriChannel> channels =
        (Map<String, GamenoriChannel>) mcs.get(MemcacheKey.CHANNELS_KEY);
    if (channels != null) {
      channel = channels.get(channelId);
    } 
    return channel;
  }

}
